package org.opencare.tmd;

import java.util.Date;

import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Document;
import org.opencare.lib.model.Constants;
import org.opencare.lib.model.ExtensionFactory;
import org.opencare.lib.model.cap.Alert;
import org.opencare.lib.model.cap.Info;
import org.opencare.lib.model.cap.Resource;
import org.opencare.lib.model.edxl.ContentObject;
import org.opencare.lib.model.edxl.EDXLDistribution;
import org.opencare.lib.model.edxl.EmbeddedXMLContent;
import org.opencare.lib.model.edxl.XmlContent;

public class EDXLBuilderImpl implements Constants, EDXLBuilder {
	private Abdera abdera;
	private Factory factory;

	private String sender;
	
	public EDXLBuilderImpl() {
		super();
		// TODO Auto-generated constructor stub

		abdera = new Abdera();
		factory = abdera.getFactory();
		factory.registerExtension(new ExtensionFactory());
	}

	@Override
	public EDXLDistribution build(String distributionID, Date pubDate, String topic, String description, String eventType,
			String eventLevel, String disasterType, String category, String link){
		// TODO Auto-generated method stub

		Document<EDXLDistribution> doc = factory.newDocument();
			
		EDXLDistribution edxl = (EDXLDistribution) factory.newElement(EDXL_EDXL_DISTRIBUTION, doc);
		edxl.setDistributionID(distributionID);
		edxl.setSenderID(this.sender);
		
		if(pubDate == null)
			edxl.setDateTimeSent(new Date());
		else
			edxl.setDateTimeSent(pubDate);
		
		if( "ACTUAL".equalsIgnoreCase(eventType)){ 
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Actual);
		}else if("EXERCISE".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Exercise);
		}else if("SYSTEM".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.System);
		}else if("TEST".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Test);
		}		
				
		edxl.setDistributionType(EDXLDistribution.DistributionType.Report);
		
		edxl.setCombinedConfidentiality("UNCLASSIFIED AND NOT SENSITIVE");
		edxl.setLanguage("TH");
				
		ContentObject contentObject = edxl.addContentObject();
				 
		XmlContent xmlContent = contentObject.setXmlContent();
		EmbeddedXMLContent embedded = xmlContent.addEmbeddedXMLContent();
		Alert alert = embedded.addAlert();

		alert.setIdentifier(distributionID);
		alert.setSender(this.sender);
		alert.setSent(new Date());
		
		//if(isPublic) 
			alert.setScope(Alert.Scope.Public);
		//else 
		//	alert.setScope(Alert.Scope.Restricted);
		
		alert.setType(Alert.Type.Alert);
				
		if( "ACTUAL".equalsIgnoreCase(eventType)){ 
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Actual);
			alert.setStatus(Alert.Status.Actual);
		}else if("EXERCISE".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Exercise);
			alert.setStatus(Alert.Status.Exercise);
		}else if("SYSTEM".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.System);
			alert.setStatus(Alert.Status.System);
		}else if("TEST".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Test);
			alert.setStatus(Alert.Status.Test);
		}
				
		// ...
		Info info = alert.addInfo();
		info.setHeadline(topic);
		info.setDescription(description);
		
		
		if("GEO".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Geo);
		else if ("MET".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Met);
		else if ("SAFETY".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Safety);
		else if ("SECURITY".equalsIgnoreCase(category))	
			info.setCategories(Info.Category.Security);
		else if ("RESCUE".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Rescue);			
		else if ("FIRE".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Fire);
		else if ("HEALTH".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Health);
		else if ("ENV".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Env);
		else if ("TRANSPORT".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Transport);
		else if ("INFRA".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Infra);
		else if ("CBRNE".equalsIgnoreCase(category))
			info.setCategories(Info.Category.CBRNE);
		else if ("OTHER".equalsIgnoreCase(category))
			info.setCategories(Info.Category.Other);
						
		info.setEvent(disasterType);
		info.setUrgency(Info.Urgency.Expected);
		info.setCertainty(Info.Certainty.Unknown);
		info.setSeverity(Info.Severity.Unknown);
		info.setLanguage("TH");
		info.addParameter("alertLevel", eventLevel);
		
		if ((link != null) && (link.length() > 0)) {
			Resource resource = info.addResource();
			resource.setDescription("Link");
			if (link != null && !link.equalsIgnoreCase(""))
				resource.setUri(link);
		}
		
/*		StringBuffer buff = new StringBuffer();
		buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		buff.append(edxl.toString());
				
		return buff.toString();*/
		
		return edxl;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}
}
